package com.news.web.servlet.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.News;
import com.news.entity.Review;
import com.news.service.NewsService;
import com.news.service.ReviewService;
import com.news.service.impl.NewsServiceImpl;
import com.news.service.impl.ReviewServiceImpl;

/**
 * Servlet implementation class NewsIndexServlet
 */
@WebServlet("/NewsIndexServlet")
public class NewsIndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 创建NewsService实例
		NewsService newsService = new NewsServiceImpl();
		// 创建ReviewService实例
		ReviewService reviewService = new ReviewServiceImpl();
		// 创建map
		Map<String, Object> indexMap = new HashMap<String, Object>();
		// 从数据库取出数据
		// 查找最热门的三条新闻
		List<News> queryNewsHeatNumList = newsService.queryNewsHeatNum(3);
		// 装入数据
		indexMap.put("queryNewsHeatNumList", queryNewsHeatNumList);
		// 获取移动设备的前五条新闻
		List<News> queryNewsTypeMobileNumList = newsService.queryNewsTypeNum("移动设备", 5);
		// 装入数据
		indexMap.put("queryNewsTypeMobileNumList", queryNewsTypeMobileNumList);
		// 获取平板电脑的前两条新闻
		List<News> queryNewsTypeTabletNumList = newsService.queryNewsTypeNum("平板电脑", 2);
		// 装入数据
		indexMap.put("queryNewsTypeTabletNumList", queryNewsTypeTabletNumList);
		// 获取产品的前四条新闻
		List<News> queryNewsTypeProductNumList = newsService.queryNewsTypeNum("产品", 4);
		// 装入数据
		indexMap.put("queryNewsTypeProductNumList", queryNewsTypeProductNumList);
		// 获取照相机的前三条新闻
		List<News> queryNewsTypeCameraNumList = newsService.queryNewsTypeNum("照相机", 3);
		// 装入数据
		indexMap.put("queryNewsTypeCameraNumList", queryNewsTypeCameraNumList);
		// 获取设计的前四条新闻
		List<News> queryNewsTypeDesignNumList = newsService.queryNewsTypeNum("设计", 4);
		// 装入数据
		indexMap.put("queryNewsTypeDesignNumList", queryNewsTypeDesignNumList);
//		查询底栏的随机三条新闻
		List<News> likeSelectListNewsNum = newsService.likeSelectListNewsNum(3);
		// 装入数据
		indexMap.put("likeSelectListNewsNum", likeSelectListNewsNum);
//		查询右侧栏热度最高的四条新闻
		List<News> queryRightNewsHeatNumList = newsService.queryNewsHeatNum(4);
		// 装入数据
		indexMap.put("queryRightNewsHeatNumList", queryRightNewsHeatNumList);
		//查询右侧栏评论最高的四条新闻
		List<News> queryNewsReviewNum = newsService.queryNewsReviewNum(4);
		// 装入数据
		indexMap.put("queryNewsReviewNum", queryNewsReviewNum);
		// 将数据存入
		request.setAttribute("indexMap", indexMap);
		// 跳转回原页面
		request.getRequestDispatcher("/News/index.jsp").forward(request, response);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
